package cn.vonce.sqlbean.example.controller;

import cn.vonce.sql.bean.Delete;
import cn.vonce.sql.helper.Cond;
import cn.vonce.sql.helper.Wrapper;
import cn.vonce.sqlbean.example.common.Result;
import cn.vonce.sqlbean.example.model.entity.User;
import cn.vonce.sqlbean.example.model.entity.sql.User$;
import cn.vonce.sqlbean.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Delete 控制器
 *
 * @author Jovi
 * @version 1.0
 * @email imjovi@qq.com
 * @date 2024/2/27 15:18
 */
@RequestMapping
@RestController
public class DeleteController {

    @Autowired
    private UserService userService;

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Long id) {
        if (userService.deleteById(id) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 删除年龄为18岁的用户
     *
     * @return
     */
    @DeleteMapping("deleteBy1")
    public Result deleteBy1() {
        if (userService.delete(new Delete().where().eq(User::getAge, 18).back()) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 删除年龄为18岁且姓名为美美或者丽丽的用户
     *
     * @return
     */
    @DeleteMapping("deleteBy2")
    public Result deleteBy2() {
        int i = userService.deleteBy(
                Wrapper.where(Cond.gt(User::getAge, 18))
                        .and(
                                Wrapper.where(Cond.like(User::getNickName, "美美"))
                                        .or(Cond.like(User::getNickName, "丽丽"))
                        )
        );
        if (i > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 删除年龄为18岁的用户
     *
     * @return
     */
    @DeleteMapping("deleteBy3")
    public Result deleteBy3() {
        if (userService.deleteBy("& = ?", User$.id, 18) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

}
